using System;
using System.Net;
using System.Xml;
using System.Runtime.Serialization.Json;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
using System.Linq;
using cIRC.Schema;
using System.Web;

namespace cIRC
{
    class cIRC
    {
        private IRC IrcObject;

        private static void BotSpeak()
        {
            string user = string.Empty;
            string message = string.Empty;
            Enumerable.Range(0, 500).ToList().ForEach(x => message += "|" + x.ToString());
            
            //if (String.IsNullOrEmpty(message) || message.Length > 1000) return;
            int currCharIndex = 0;
            int charCountLimit = 450;
            int messageLength = message.Length;
            while (currCharIndex < messageLength)
            {
                charCountLimit = (currCharIndex + charCountLimit < messageLength)?
                charCountLimit:
                messageLength - currCharIndex;
                if (String.IsNullOrEmpty(user))
                {

                    //Console.WriteLine("Sending: {0}", message);
                    Console.WriteLine(message.Substring(currCharIndex, charCountLimit));
                }
                else
                {
                    //Console.WriteLine("Sending to {0}, {1}", user, message);
                    Console.WriteLine(String.Format("NOTICE {0} :{0}, {1}!", user, message.Substring(currCharIndex, currCharIndex + charCountLimit)));
                }
                currCharIndex += charCountLimit;
            }
            //this.IrcWriter.Flush();
        }

        static void Main(string[] args)
        {
            //BotSpeak();
            Console.Write("Server: ");
            string IrcServer = Console.ReadLine();
            IrcServer = (String.IsNullOrEmpty(IrcServer)) ? "irc.freenode.net" : IrcServer;
            //Console.Write("Port: ");
            int IrcPort = 6667;// Convert.ToInt32(Console.ReadLine());
            //Console.Write("User: ");
            string IrcUser = "nhelp";// Console.ReadLine();
            Console.Write("Chan: ");
            string IrcChan = Console.ReadLine();
            IrcChan = (String.IsNullOrEmpty(IrcChan)) ? "#testbot" : IrcChan;

            cIRC IrcApp = new cIRC(IrcServer, IrcPort, IrcUser, IrcChan);
        } /* Main */

        private cIRC(string IrcServer, int IrcPort, string IrcUser, string IrcChan)
        {
            //IrcObject = new IRC("CumpsD", "#mypreciousss");
            IrcObject = new IRC(IrcUser, IrcChan);

            // Assign events
            IrcObject.eventReceiving += new CommandReceived(IrcCommandReceived);
            IrcObject.eventTopicSet += new TopicSet(IrcTopicSet);
            IrcObject.eventTopicOwner += new TopicOwner(IrcTopicOwner);
            IrcObject.eventNamesList += new NamesList(IrcNamesList);
            IrcObject.eventServerMessage += new ServerMessage(IrcServerMessage);
            IrcObject.eventJoin += new Join(IrcJoin);
            IrcObject.eventPart += new Part(IrcPart);
            IrcObject.eventMode += new Mode(IrcMode);
            IrcObject.eventNickChange += new NickChange(IrcNickChange);
            IrcObject.eventKick += new Kick(IrcKick);
            IrcObject.eventQuit += new Quit(IrcQuit);

            // Connect to server
            //IrcObject.Connect("efnet.xs4all.nl", 6667);	
            IrcObject.Connect(IrcServer, IrcPort);
        } /* cIRC */

        private void IrcCommandReceived(string IrcCommand)
        {
            //Console.WriteLine(IrcCommand);
        } /* IrcCommandReceived */

        private void IrcTopicSet(string IrcChan, string IrcTopic)
        {
            Console.WriteLine(String.Format("Topic of {0} is: {1}", IrcChan, IrcTopic));
        } /* IrcTopicSet */

        private void IrcTopicOwner(string IrcChan, string IrcUser, string TopicDate)
        {
            Console.WriteLine(String.Format("Topic of {0} set by {1} on {2} (unixtime)", IrcChan, IrcUser, TopicDate));
        } /* IrcTopicSet */

        private void IrcNamesList(string UserNames)
        {
            Console.WriteLine(String.Format("Names List: {0}", UserNames));
        } /* IrcNamesList */

        private void IrcServerMessage(string ServerMessage)
        {
            //Console.WriteLine(String.Format("Server Message: {0}", ServerMessage));
        } /* IrcNamesList */

        private void IrcJoin(string IrcChan, string IrcUser)
        {
            Console.WriteLine(String.Format("{0} joins {1}", IrcUser, IrcChan));
            if (!IrcUser.ToLowerInvariant().Contains(IrcObject.MasterUser.ToLowerInvariant()))
                return;
            IrcObject.IrcWriter.WriteLine(String.Format("NOTICE {0} :Hello {0}, welcome to {1}!", IrcUser, IrcChan));
            IrcObject.IrcWriter.Flush();
        } /* IrcJoin */

        private void IrcPart(string IrcChan, string IrcUser)
        {
            Console.WriteLine(String.Format("{0} parts {1}", IrcUser, IrcChan));
        } /* IrcPart */

        private void IrcMode(string IrcChan, string IrcUser, string UserMode)
        {
            if (IrcUser != IrcChan)
            {
                Console.WriteLine(String.Format("{0} sets {1} in {2}", IrcUser, UserMode, IrcChan));
            }
        } /* IrcMode */

        private void IrcNickChange(string UserOldNick, string UserNewNick)
        {
            Console.WriteLine(String.Format("{0} changes nick to {1}", UserOldNick, UserNewNick));
        } /* IrcNickChange */

        private void IrcKick(string IrcChannel, string UserKicker, string UserKicked, string KickMessage)
        {
            Console.WriteLine(String.Format("{0} kicks {1} out {2} ({3})", UserKicker, UserKicked, IrcChannel, KickMessage));
        } /* IrcKick */

        private void IrcQuit(string UserQuit, string QuitMessage)
        {
            Console.WriteLine(String.Format("{0} has quit IRC ({1})", UserQuit, QuitMessage));
        } /* IrcQuit */
    } /* cIRC */
} /* cIRC */